package src.ee.ioc.smaug.fsm;

/**
 * Moore machine as one of FSM
 * 
 * @author Margarita Spichakova
 * 
 */
public class MooreMachine extends FiniteStateMachine {

    /*
     * (non-Javadoc)
     * 
     * @see src.ee.ioc.smaug.fsm.FiniteStateMachine#makeTransition(char)
     */
    public void makeTransition(char inputChar) {
        Transition tr = getTransition(transitionTable[currentState.getStateID()][this
                .getInputAlphabet().findPosition(inputChar)]);
        if (tr == null) {
            this.setRunning(false);
            System.err.println("Machine stopped, transition not found");
            return;
        } else {
            currentState = tr.getToState();
            if (!currentState.isActive()) {
                this.setRunning(false);
                return;
            }
            setOutString(getOutString() + tr.getToState().getStateValue());
        }
    }

   

    /*
     * (non-Javadoc)
     * 
     * @see src.ee.ioc.smaug.fsm.FiniteStateMachine#runMachine()
     */
    public void runMachine() {
        this.setOutString("");
        currentState = states.get(0);
        this.setRunning(true);
        if (!currentState.isActive()) {
            return;
        }
        setOutString(getOutString() + currentState.getStateValue());

        for (int i = 0; i < inString.length(); i++) {
            if (!this.isRunning()) {
                return;
            } else {
                makeTransition(inString.charAt(i));
            }
        }

    }
}
